home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / text_utl / text2 / text2.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-01-20  |  11.9 KB  |  318 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "Form1"
  6.    ClientHeight    =   6120
  7.    ClientLeft      =   1515
  8.    ClientTop       =   1995
  9.    ClientWidth     =   8535
  10.    ClipControls    =   0   'False
  11.    FontBold        =   -1  'True
  12.    FontItalic      =   0   'False
  13.    FontName        =   "Times New Roman"
  14.    FontSize        =   9.75
  15.    FontStrikethru  =   0   'False
  16.    FontUnderline   =   0   'False
  17.    Height          =   6900
  18.    Left            =   1410
  19.    LinkTopic       =   "Form1"
  20.    MaxButton       =   0   'False
  21.    ScaleHeight     =   408
  22.    ScaleMode       =   3  'Pixel
  23.    ScaleWidth      =   569
  24.    Top             =   1320
  25.    Width           =   8745
  26.    Begin HScrollBar sc2 
  27.       Height          =   255
  28.       LargeChange     =   20
  29.       Left            =   300
  30.       Max             =   240
  31.       SmallChange     =   6
  32.       TabIndex        =   2
  33.       Top             =   5580
  34.       Width           =   7665
  35.    End
  36.    Begin CommonDialog CMDialog1 
  37.       Left            =   75
  38.       Top             =   -120
  39.    End
  40.    Begin VScrollBar sc 
  41.       Height          =   5310
  42.       LargeChange     =   322
  43.       Left            =   7950
  44.       TabIndex        =   0
  45.       Top             =   285
  46.       Width           =   255
  47.    End
  48.    Begin PictureBox p 
  49.       BackColor       =   &H00FFFFFF&
  50.       FontBold        =   -1  'True
  51.       FontItalic      =   0   'False
  52.       FontName        =   "Times New Roman"
  53.       FontSize        =   10.5
  54.       FontStrikethru  =   0   'False
  55.       FontUnderline   =   0   'False
  56.       Height          =   5310
  57.       Left            =   300
  58.       ScaleHeight     =   352
  59.       ScaleMode       =   3  'Pixel
  60.       ScaleWidth      =   509
  61.       TabIndex        =   1
  62.       TabStop         =   0   'False
  63.       Top             =   285
  64.       Width           =   7665
  65.    End
  66.    Begin Shape box 
  67.       BorderColor     =   &H00000000&
  68.       FillColor       =   &H00800000&
  69.       FillStyle       =   0  'Solid
  70.       Height          =   240
  71.       Left            =   7950
  72.       Top             =   5595
  73.       Width           =   255
  74.    End
  75.    Begin Menu mnuFile 
  76.       Caption         =   "File"
  77.       Begin Menu mnuOpen 
  78.          Caption         =   "Open"
  79.       End
  80.       Begin Menu mnuFont 
  81.          Caption         =   "Font"
  82.       End
  83.    End
  84. Option Explicit
  85. Dim lArray() As String * 100  ' you can make the line width anything you want I made it 100 characters wide
  86. Dim linenum%, texwidth%, h%, paint%, oldx%, oldy%, opening%, fileopen%
  87. Declare Function TextOut Lib "GDI" (ByVal hDC As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal lpString As String, ByVal nCount As Integer) As Integer
  88. Declare Sub ScrollWindow Lib "User" (ByVal hWnd As Integer, ByVal XAmount As Integer, ByVal YAmount As Integer, ByVal lpRect As Long, ByVal lpClipRect As Long)
  89. Declare Sub UpdateWindow Lib "User" (ByVal hWnd As Integer)
  90. Declare Function GetSysColor Lib "User" (ByVal nIndex%) As Long
  91. Const COLOR_SCROLLBAR = 0
  92. Sub Center (frm As Form)
  93.    frm.Top = (Screen.Height - frm.Height) \ 2
  94.    frm.Left = (Screen.Width - frm.Width) \ 2
  95. End Sub
  96. Sub ChangeFont ()
  97. Dim i%
  98.  p.Cls
  99.     h = p.TextHeight(lArray(1))
  100.     sc2.Max = max(0, (p.TextWidth("a") * texwidth) - (p.Width \ 2))
  101.     If sc2.Value > sc2.Max Then sc2.Value = sc2.Max: oldy = sc2.Value
  102.     sc.LargeChange = Format(p.Height / h, "0")  'set to the number of lines in one page
  103.     sc.Max = max(0, linenum - (sc.LargeChange - 2)) 'set the max to the number of lines - one page - one line
  104.     sc.LargeChange = min(linenum, Format(p.Height / h, "0")) ' if number of lines are less than a page
  105.     If sc.Value > sc.Max Then sc.Value = sc.Max   ' make sure the value isn't past max when you change the font
  106.     oldx = sc.Value
  107.  opening = False
  108. End Sub
  109. Sub Form_Load ()
  110. Dim oneline$, i%, Color&
  111. Center Me
  112. Color = GetSysColor(COLOR_SCROLLBAR)
  113. box.FillColor = Color       ' set the box the same color as your scroll bars
  114. End Sub
  115. Sub Form_Paint ()
  116. Dim Color&
  117.   paint = True    ' repaint the whole picture box only when the form repaints not when the picture box repaints
  118.   p.Refresh       ' or otherwise the picture repaints when you scroll which slows down the scrolling
  119.   Color = GetSysColor(COLOR_SCROLLBAR)  'this is only if you change the color of the scroll bars while the program is running
  120.   box.FillColor = Color                 'so the box is the same color as the scroll bars
  121. End Sub
  122. Function max% (a%, b%)
  123.   If a >= b Then max = a
  124.   If b > a Then max = b
  125. End Function
  126. Function min% (a%, b%)
  127. If a <= b Then min = a
  128. If b < a Then min = b
  129. End Function
  130. Sub mnuFont_Click ()
  131. On Error Resume Next
  132. CMDialog1.CancelError = True
  133. CMDialog1.Flags = &H103&
  134. If Int(p.FontSize) < p.FontSize Then CMDialog1.FontSize = Int(p.FontSize) + 1:  Else CMDialog1.FontSize = p.FontSize  ' if the font in the picture box is in between numbers round up
  135. CMDialog1.FontName = p.FontName
  136. CMDialog1.FontBold = p.FontBold
  137. CMDialog1.Color = p.ForeColor
  138. CMDialog1.FontItalic = p.FontItalic
  139. CMDialog1.Action = 4
  140. If Err = 0 Then 'reset the picture box font attributes
  141.    p.FontSize = CMDialog1.FontSize
  142.    p.FontName = CMDialog1.FontName
  143.    p.FontBold = CMDialog1.FontBold
  144.    p.ForeColor = CMDialog1.Color
  145.    p.FontItalic = CMDialog1.FontItalic
  146.    opening = True
  147.     ChangeFont
  148. End If
  149. End Sub
  150. Sub mnuOpen_Click ()
  151. Dim fname$
  152. On Error Resume Next
  153. CMDialog1.CancelError = True
  154. CMDialog1.DefaultExt = "TXT"
  155. CMDialog1.Filter = "Text Files (.txt)| *.txt|All Files (*.*)| *.*"
  156. CMDialog1.DialogTitle = "Open File"
  157. CMDialog1.Action = 1
  158. If Err = 0 Then
  159.    opening = True
  160.    fileopen = True
  161.    fname = CMDialog1.Filename
  162.    OpenFile fname
  163. End If
  164. End Sub
  165. 'this function paints two lines of text on the picture starting from how many lines
  166. 'there are in a page - 2 to how many lines in a page. The sc.LargeChange has been
  167. 'set to how many lines in a page
  168. Sub NextLine ()
  169. Dim d%, i%, c%, pos%
  170. c = sc.Value   ' set c to the value of the scroll bar so you know what line you are on
  171. p.CurrentY = (sc.LargeChange - 2) * h  ' how many lines in a page - 2 * the height of one line
  172. For i = c + (sc.LargeChange - 2) To c + sc.LargeChange ' set I so it is the bottom two lines on the page
  173.   p.CurrentX = -sc2.Value       ' make current x  the value of the horizontal scroll bar so if you have scrolled left then it starts in the right place
  174.   p.Print lArray(i)
  175. End Sub
  176. 'this function opens a file, counts the number of lines, sets the line height for the size
  177. 'font your using at the time,gets the widest line so you can set the width, sets both scroll bars max,
  178. 'redims the line array to the size of the file, fills the array with the lines, and then prints the first page
  179. Sub OpenFile (fname$)
  180. Dim oneline$, i%, w%
  181.  p.Cls
  182. sc.Value = 0
  183. oldx = sc.Value
  184. sc2.Value = 0
  185. oldy = sc2.Value
  186. texwidth = 0
  187. Open fname For Input As #1
  188. linenum = 0
  189. Do While Not EOF(1)
  190.   Line Input #1, oneline
  191.    w = Len(oneline)
  192.    If w >= texwidth Then texwidth = w
  193.    linenum = linenum + 1     ' get the number of lines
  194. sc2.Max = max(0, (p.TextWidth("a") * texwidth) - (p.Width \ 2))
  195. ReDim lArray(linenum + 2)    'set to two more than number of lines so no errors when scrolling
  196. Seek #1, 1
  197.   Line Input #1, oneline
  198.   h = p.TextHeight(oneline)  ' get the height of one line
  199.   sc.SmallChange = 1              ' set to one line
  200.   sc.LargeChange = Format(p.Height / h, "0")    'set to the number of lines in one page
  201.   sc.Max = max(0, linenum - (sc.LargeChange - 2))   'set the max to the number of lines - one page - one line
  202.   sc.LargeChange = min(linenum, Format(p.Height / h, "0"))  ' if number of lines are less than a page
  203.  lArray(0) = ""    ' for a margin on the top
  204. Seek #1, 1
  205. For i = 1 To linenum
  206.   Line Input #1, oneline
  207.   lArray(i) = "     " & oneline  ' fill line array with each line of text I've added a margin on the left for looks
  208. For i = linenum + 1 To linenum + 2
  209.   lArray(i) = ""  ' fill last two with empty strings
  210. Close #1
  211. opening = False
  212. End Sub
  213. Sub p_Paint ()
  214. If paint Then prn: paint = False   ' the form is repainting
  215. End Sub
  216. ' this function just prints the previous line at the top of the page
  217. Sub PrevLine ()
  218. Dim d%, i%, c%, pos%
  219. c = sc.Value
  220. p.CurrentX = -sc2.Value
  221. p.CurrentY = 0
  222. For i = c To c
  223.   p.Print lArray(i)
  224. End Sub
  225. ' this function paints the whole page of text by which ever page you are on
  226. Sub prn ()
  227. If Not fileopen Then Exit Sub
  228. Dim d%, i%, c%
  229. c = sc.Value
  230. p.CurrentY = 0
  231. For i = c To c + sc.LargeChange    ' set to one page
  232.   p.CurrentX = -sc2.Value          ' set to the value of the horizontal scroll bar
  233.   p.Print lArray(i)
  234. End Sub
  235. Sub sc_Change ()
  236. If Not fileopen Or opening Then Exit Sub  ' don't want to do anything if there is no file loaded or are loading a file
  237.      
  238.      If oldx < sc.Value And oldx + 1 = sc.Value Then  ' if just scrolling down one line
  239.         ScrollWindow p.hWnd, 0, -((sc.Value - oldx) * h), 0, 0  ' if scrolling down, negative values
  240.         UpdateWindow p.hWnd      ' force an update or you can't print on the scrolled area right away
  241.         NextLine
  242.      End If
  243.      
  244.      If oldx > sc.Value And oldx - 1 = sc.Value Then    ' if just scrolling up one line
  245.         ScrollWindow p.hWnd, 0, ((oldx - sc.Value) * h), 0, 0  ' if scrolling up, positive values
  246.         UpdateWindow p.hWnd      ' force an update or you can't print on the scrolled area right away
  247.         PrevLine
  248.      End If
  249.      
  250.      If oldx < sc.Value And oldx + 1 < sc.Value Then   ' if scrolling down a page
  251.         ScrollWindow p.hWnd, 0, -((sc.Value - oldx) * h), 0, 0
  252.         UpdateWindow p.hWnd     ' force an update or you can't print on the scrolled area right away
  253.         prn
  254.      End If
  255.      
  256.      If oldx > sc.Value And oldx - 1 > sc.Value Then   'if scrolling up a page
  257.         ScrollWindow p.hWnd, 0, ((oldx - sc.Value) * h), 0, 0
  258.         UpdateWindow p.hWnd     ' force an update or you can't print on the scrolled area right away
  259.         prn
  260.      End If
  261.      oldx = sc.Value
  262. End Sub
  263. Sub sc_Scroll ()
  264. If Not fileopen Then Exit Sub
  265.      On Error GoTo errorhandler  ' I was getting a error on large files if I was using
  266.      If oldx < sc.Value Then     ' the scroll thumb and the mouse pointer went off the scroll bar this fixed it
  267.         ScrollWindow p.hWnd, 0, -((sc.Value - oldx) * h), 0, 0
  268.         UpdateWindow p.hWnd
  269.         prn                   ' I had to print a whole page at a time because if I tried to print one
  270.         oldx = sc.Value       ' line at a time it wouldn't keep up with the scroll bar and the print would get screwed up
  271.      End If
  272.      
  273.      If oldx > sc.Value Then
  274.         ScrollWindow p.hWnd, 0, ((oldx - sc.Value) * h), 0, 0
  275.         UpdateWindow p.hWnd
  276.         prn
  277.         oldx = sc.Value
  278.      End If
  279. Exit Sub
  280. errorhandler:
  281. oldx = sc.Value
  282. p.Cls
  283. Exit Sub
  284. Resume Next
  285. End Sub
  286. Sub sc2_Change ()
  287. If Not fileopen Or opening Then Exit Sub ' don't want to do anything if there is no file loaded or are loading a file
  288.      
  289.      If oldy < sc2.Value Then
  290.         ScrollWindow p.hWnd, -(sc2.Value - oldy), 0, 0, 0 'if scrolling to the left, negative values
  291.         UpdateWindow p.hWnd
  292.         prn
  293.      End If
  294.      
  295.      If oldy > sc2.Value Then
  296.         ScrollWindow p.hWnd, (oldy - sc2.Value), 0, 0, 0 'if scrolling to the right, positive values
  297.         UpdateWindow p.hWnd
  298.         prn
  299.      End If
  300.    oldy = sc2.Value
  301. End Sub
  302. Sub sc2_Scroll ()
  303. If Not fileopen Then Exit Sub  ' don't want to do anything if there is no file loaded
  304.      
  305.      If oldy < sc2.Value Then
  306.         ScrollWindow p.hWnd, -(sc2.Value - oldy), 0, 0, 0
  307.         UpdateWindow p.hWnd
  308.         prn
  309.      End If
  310.      
  311.      If oldy > sc2.Value Then
  312.         ScrollWindow p.hWnd, (oldy - sc2.Value), 0, 0, 0
  313.         UpdateWindow p.hWnd
  314.         prn
  315.      End If
  316.    oldy = sc2.Value
  317. End Sub
  318.